package com.briup.corejava.day13;

import java.util.*;

/**
 * 斗地主发牌测试
 */
public class Test4 {
    // 权重的静态map
    public static final Map<String,Integer> map;
    static{
        map = new HashMap<>();
        map.put("3",1);
        map.put("4",2);
        map.put("5",3);
        map.put("6",4);
        map.put("7",5);
        map.put("8",6);
        map.put("9",7);
        map.put("10",8);
        map.put("J",9);
        map.put("Q",10);
        map.put("K",11);
        map.put("A",12);
        map.put("2",13);
        map.put("joker",14);
        map.put("JOKER",15);
    }

    public static void main(String[] args) {
        // 组牌
        List<Card> list = new ArrayList<>();
        // 点数的数组
        List<String> numbers = new ArrayList<>();
        Collections.addAll(numbers, "A", "2", "3", "4", "5",
                "6", "7", "8", "9", "10", "J", "Q", "K");
        // 花色的数组
        Card.Color[] values = Card.Color.values();
        // 双层循环
        for(String s:numbers){
            for(Card.Color c : values){
                Card card = new Card();
                card.setNum(s);
                card.setColor(c);
                card.setH(map.get(card.getNum()));
                list.add(card);
            }
        }
        // 定义两张joker牌
        Card c1 = new Card(null,"joker");
        c1.setH(14);
        Card c2 = new Card(null,"JOKER");
        c2.setH(15);
        Collections.addAll(list,c1,c2);
        // 洗牌
        Collections.shuffle(list);
        System.out.println(list);

        // 准备三个玩家
        Player p1 = new Player("张三");
        Player p2 = new Player("李四");
        Player p3 = new Player("龙傲天");

        // 底牌先取出来
        List<Card> dp = list.subList(51, 54);
        System.out.println(dp);
        // 取【10-40】的随机数
        Random random = new Random();
        // i的取值
        // 0:p1 , 1:p2, 2:p3
        int pi = (random.nextInt(31)+10)%3;
        if(pi % 3 ==0){
            p1.setRole("地主");
            p1.getCards().addAll(dp);
        }else if(pi % 3 == 1){
            p2.setRole("地主");
            p2.getCards().addAll(dp);
        }else {
            p3.setRole("地主");
            p3.getCards().addAll(dp);
        }
        // 发牌
        for(int i=0;i<51;i++){
            if(i % 3 ==0){
                p1.getCards().add(list.get(i));
            }else if(i % 3 == 1){
                p2.getCards().add(list.get(i));
            }else {
                p3.getCards().add(list.get(i));
            }
        }
        // 比较器
        Comparator<Card> cardComparator =  new Comparator<Card>(){
            @Override
            public int compare(Card o1, Card o2) {
                // 先判断点数 点数相同再判断花色
                if(Objects.equals(o1.getNum(), o2.getNum()))
                    return o2.getColor().getH()-o1.getColor().getH();
                return o1.getH()-o2.getH();
            }
        };
        // 使用比较器调用sort方法
        Collections.sort(p1.getCards(),cardComparator);
        Collections.sort(p2.getCards(),cardComparator);
        p3.getCards().sort(cardComparator);

        // 展示一下手里的牌
        System.out.println(p1);
        System.out.println(p2);
        System.out.println(p3);

    }
}
// 玩家的类
class Player{
    /**
     * 姓名
     */
    private String name;
    /**
     * 牌
     */
    private List<Card> cards;
    /**
     * 角色
     */
    private String role;

    public Player() {
    }

    public Player(String name) {
        this.name = name;
        this.cards = new ArrayList<>();
        this.role = "农民";
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public List<Card> getCards() {
        return cards;
    }

    public void setCards(List<Card> cards) {
        this.cards = cards;
    }

    public String getRole() {
        return role;
    }

    public void setRole(String role) {
        this.role = role;
    }

    @Override
    public String toString() {
        return name+" ["+role+"] 手牌为:\n"+cards;
    }
}
// 牌的类
class Card {
    enum Color{
        SPADE("♠️",4),
        HEART("♥️",3),
        CLUB("♣️",2),
        DIAMOND("♦️",1);
        private String desc; // 描述
        private int h; // 权重
        Color(String desc , int h){
            this.desc = desc;
            this.h = h;
        }

        public int getH() {
            return h;
        }

        public String getDesc() {
            return desc;
        }
    }
    private Color color;
    private String num;
    private int h;
    public Card(){}
    public Card(Color color, String num) {
        this.color = color;
        this.num = num;
    }
    public Color getColor() {
        return color;
    }

    public void setH(int h) {
        this.h = h;
    }

    public int getH() {
        return h;
    }

    public void setColor(Color color) {
        this.color = color;
    }
    public String getNum() {
        return num;
    }

    public void setNum(String num) {
        this.num = num;
    }

    @Override
    public String toString() {
        return Objects.isNull(color)
                ?getNum()
                :color.getDesc()+getNum();
    }
}
